package simple.framework.util;

import java.io.*;
import java.net.URLEncoder;
import java.net.URLDecoder;
import java.util.Map;
import java.util.regex.Pattern;
import javax.servlet.http.*;
import javax.servlet.ServletContext;

public class conf
{
  public sp sp;
  public common common;
  public HttpSession session;
  public ServletContext application;
  public HttpServletRequest request;
  public HttpServletResponse response;

  public String ajaxPreContent = "<!--jtbc-->";
  public String appName = "";
  public String adminFolder = "";
  public String charset = "";
  public String convert = "";
  public String connStr = "";
  public String dbtype = "";
  public String default_language = "";
  public String default_template = "";
  public String default_theme = "";
  public String imagesRoute = "";
  public String codeRoute ="";
  public String isApp = "";
  public String jtbccinfo = "<!--jtbccinfo-->";
  public String linkMode = "";
  public String navSpStr = "";
  public String ntitle = "";
  public String ntitleSpStr = "";
  public String[][] njtbcelement = null;
  public String repath = "";
  public Object[][] rsAry = null;
  public Object[][] rsbAry = null;
  public Object[][] rscAry = null;
  public Object[][] rstAry = null;
  public String sysName = "jtbc";
  public String xmlsfx = "";
  public Map connMap = null;
  public String isDebug = null;
  
  public void cntitle(String argString)
  {
    String tString = argString;
    if (cls.isEmpty(ntitle)) ntitle = encode.htmlencode(tString);
    else ntitle = encode.htmlencode(tString) + ntitleSpStr + ntitle;
  }

  public String decodeParameter(String argString)
  {
    String tString = argString;
    if (!cls.isEmpty(tString))
    {
      if (convert.equals("1"))
      {
        try
        {
          tString = new String(tString.getBytes("utf-8"), charset);
        }
        catch(Exception e) {
            e.printStackTrace();
        }
      }
    }
    return tString;
  }

  public String getAppKey(String argString)
  {
    String tmpstr = "";
    String tString = argString;
    tmpstr = appName + tString;
    return tmpstr;
  }

  public String getActiveThings(String argString)
  {
    String tmpstr = "";
    String tthing = "";
    String tdthing = "";
    String tString = argString;
    if (tString.equals("lng"))
    {
      tthing = "language";
      tdthing = default_language;
    }
    else if (tString.equals("sel"))
    {
      tthing = "language";
      tdthing = default_language;
    }
    else if (tString.equals("tpl"))
    {
      tthing = "template";
      tdthing = default_template;
    }
    else if (tString.equals("theme"))
    {
      tthing = "theme";
      tdthing = default_theme;
    }
    else
    {
      tthing = "language";
      tdthing = default_language;
    }
    String tnthing = cookies.getAttribute(this, getAppKey("config-" + tthing));
    tnthing = cls.getString(tnthing);
    if (!cls.isEmpty(tnthing)) tmpstr = encode.htmlencode(tnthing);
    else tmpstr = encode.htmlencode(tdthing);
    return tmpstr;
  }

  public String getActualRoute(String argRoutestr)
  {
    String tmpstr = "";
    String tRoutestr = argRoutestr;
    tmpstr = getActualRoute(tRoutestr, getNroute());
    return tmpstr;
  }

  public String getActualRouteB(String argRoutestr)
  {
    String tmpstr = "";
    String tRoutestr = argRoutestr;
    tmpstr = getActualRoute(tRoutestr, getNroute(), "0");
    return tmpstr;
  }

  public String getActualRoute(String argRoutestr, String argNroute)
  {
    String tmpstr = "";
    String tRoutestr = argRoutestr;
    String tNroute = argNroute;
    tmpstr = getActualRoute(tRoutestr, tNroute, "1");
    return tmpstr;
  }

  public String getActualRoute(String argRoutestr, String argNroute, String argUlinkMode)
  {
    String tmpstr = "";
    String tRoutestr = argRoutestr;
    String tNroute = argNroute;
    String tUlinkMode = argUlinkMode;
    String tlinkMode = linkMode;
    if (tUlinkMode.equals("1") && !cls.isEmpty(tlinkMode)) tmpstr = tlinkMode + tRoutestr;
    else
    {
      if (tNroute.equals("greatgrandchild")) tmpstr = "../../../../" + tRoutestr;
      else if (tNroute.equals("grandchild")) tmpstr = "../../../" + tRoutestr;
      else if (tNroute.equals("child")) tmpstr = "../../" + tRoutestr;
      else if (tNroute.equals("node")) tmpstr = "../" + tRoutestr;
      else tmpstr = tRoutestr;
    }
    return tmpstr;
  }

  public String getActiveGenre(String argType, String argPath)
  {
    String tType = argType;
    String tPath = argPath;
    String tmpstr = "";
    String tCacheString = "active_genre_" + tType;
    String tActiveGenre = (String)application.getAttribute(getAppKey(tCacheString));
    if (!cls.isEmpty(tActiveGenre)) tmpstr = tActiveGenre;
    else
    {
      tmpstr = getActiveGenre(argType, tPath, null);
      if (isApp.equals("1")) application.setAttribute(getAppKey(tCacheString), tmpstr);
    }
    return tmpstr;
  }

  public String getActiveGenre(String argType, String argPath, String argFDir)
  {
    String tType = argType;
    String tPath = argPath;
    String tFDir = argFDir;
    String tKeyFile = tType;
    String tKeyFileMode = "";
    String tActiveGenre = "";
    String tActiveGenre2 = "";
    String tDirName = "";
    String tFullDirName = "";
    File tDirectoryFile = new File(application.getRealPath(getMapPath(tPath)).toString());
    File[] tlistFiles = tDirectoryFile.listFiles();
    for(int ti = 0; ti < tlistFiles.length; ti ++)
    {
      if (tlistFiles[ti].isDirectory())
      {
        tDirName = tlistFiles[ti].getName();
        if (tDirName.indexOf("=") == -1 && tDirName.indexOf("&") == -1 && tDirName.indexOf("'") == -1)
        {
          tKeyFileMode = sp.getXRootAtt(tPath + tDirName + "/common/" + tKeyFile + xmlsfx, "mode");
          if (!cls.isEmpty(tKeyFileMode))
          {
            tFullDirName = tDirName;
            if (!cls.isEmpty(tFDir)) tFullDirName = tFDir + "/" + tDirName;
            tActiveGenre += tFullDirName + "|";
            tActiveGenre2 = getActiveGenre(tType, tPath + tDirName + "/", tFullDirName);
            if (!cls.isEmpty(tActiveGenre2)) tActiveGenre += tActiveGenre2 + "|";
          }
        }
      }
    }
    tActiveGenre = cls.getLRStr(tActiveGenre, "|", "leftr");
    tActiveGenre = getActiveGenreOrdered(tFDir, tActiveGenre);
    return tActiveGenre;
  }

  public String getActiveGenreOrdered(String argDir, String argGenreString)
  {
    String tDir = argDir;
    String tGenreString = argGenreString;
    String tmpstr = "";
    if (!cls.isEmpty(tGenreString))
    {
      String tOrderString = "";
      if (cls.isEmpty(tDir)) tOrderString = sp.itake("global.config.genre-order", "cfg");
      else tOrderString = sp.itake("global." + tDir + ":config.genre-order", "cfg");
      if (!cls.isEmpty(tOrderString))
      {
        String tsGenreString = "";
        String tsOrderString = "";
        String tNewOrderString = "";
        String[] tOrderStringAry = tOrderString.split(Pattern.quote(","));
        String[] tGenreStringAry = tGenreString.split(Pattern.quote("|"));
        for (int ti = 0; ti < tOrderStringAry.length; ti ++)
        {
          tsOrderString = tOrderStringAry[ti];
          if (!cls.isEmpty(tDir)) tsOrderString = tDir + "/" + tsOrderString;
          if (cls.cinstr(tGenreString, tsOrderString, "|"))
          {
            for (int tis = 0; tis < tGenreStringAry.length; tis ++)
            {
              tsGenreString = tGenreStringAry[tis];
              if (tsGenreString.equals(tsOrderString)) tNewOrderString += tsGenreString + "|";
            }
          }
        }
        for (int tis = 0; tis < tGenreStringAry.length; tis ++)
        {
          if (!cls.cinstr(tNewOrderString, tGenreStringAry[tis], "|")) tNewOrderString += tGenreStringAry[tis] + "|";
        }
        tNewOrderString = cls.getLRStr(tNewOrderString, "|", "leftr");
        tmpstr = tNewOrderString;
      }
      else tmpstr = tGenreString;
    }
    return tmpstr;
  }

  public String getMapPath(String argRoutestr)
  {
    String tmpstr = "";
    String tNURI = getNURI();
    String tRoutestr = argRoutestr;
    Integer tState = 0;
    tNURI = cls.getLRStr(tNURI, "/", "leftr");
    do
    {
      if (tRoutestr.length() < 3) tState = 1;
      else
      {
        String tRoutestrL3 = tRoutestr.substring(0, 3);
        if (tRoutestrL3.equals("../"))
        {
          tRoutestr = cls.getLRStr(tRoutestr, "../", "rightr");
          tNURI = cls.getLRStr(tNURI, "/", "leftr");
        }
        else tState = 1;
      }
    } while (tState == 0);
    tmpstr = tNURI + "/" + tRoutestr;
    return tmpstr;
  }

  public String getNURI()
  {
    String tmpstr = "";
    tmpstr = request.getServletPath();
    return tmpstr;
  }

  public String getNURS()
  {
    String tmpstr = "";
    tmpstr = request.getQueryString();
    if (tmpstr == null) tmpstr = "";
    return tmpstr;
  }

  public String getNURL()
  {
    String tmpstr = "";
    String tNURS = getNURS();
    tmpstr = getNURI();
    if (!cls.isEmpty(tNURS)) tmpstr += "?" + tNURS;
    return tmpstr;
  }

  public String getNURLPre()
  {
    String tmpstr = "";
    String tRequestURL = request.getRequestURL().toString();
    String tNURIString = cls.getLRStr(getNURI(), "/", "leftr");
    tmpstr = cls.getLRStr(tRequestURL, "/", "leftr");
    if (!cls.isEmpty(tNURIString)) tmpstr = cls.getLRStr(tmpstr, tNURIString, "leftr");
    return tmpstr;
  }

  public String getNLng()
  {
    String tnlng = "";
    tnlng = common.getLngID(getActiveThings("lng"));
    if (cls.getNum(tnlng, -1) == -1) tnlng = "0";
    return tnlng;
  }

  public String getNroute()
  {
    String tnroute = "";
    String tmyURL = getNURI();
    String tmyURLKey = encode.base64encode(cls.getLRStr(tmyURL, "/", "leftr").getBytes());
    String tmyDir = cls.getLRStr(cls.getLRStr(tmyURL, "/", "leftr"), "/", "right");
    if (cls.isEmpty(tmyDir)) tmyDir = ":root";
    tmyURLKey += encode.base64encode(tmyDir.getBytes());
    tnroute = getNroute(tmyURLKey);
    if (cls.isEmpty(tnroute))
    {
      File tFile1 = new File(application.getRealPath(getMapPath("common/spc.guide")).toString());
      if (tFile1.exists()) tnroute = "root";
      else
      {
        File tFile2 = new File(application.getRealPath(getMapPath("../common/spc.guide")).toString());
        if (tFile2.exists()) tnroute = "node";
        else
        {
          File tFile3 = new File(application.getRealPath(getMapPath("../../common/spc.guide")).toString());
          if (tFile3.exists()) tnroute = "child";
          else
          {
            File tFile4 = new File(application.getRealPath(getMapPath("../../../common/spc.guide")).toString());
            if (tFile4.exists()) tnroute = "grandchild";
            else
            {
              File tFile5 = new File(application.getRealPath(getMapPath("../../../../common/spc.guide")).toString());
              if (tFile5.exists()) tnroute = "greatgrandchild";
            }
          }
        }
      }
      String[][] tAry = new String[1][2];
      tAry[0][0] = tmyURLKey;
      tAry[0][1] = tnroute;
      if (isApp.equals("1")) application.setAttribute(getAppKey("route"), cls.mergeAry((String[][])application.getAttribute(getAppKey("route")), tAry));
    }
    return tnroute;
  }

  public String getNroute(String argKey)
  {
    String[][] tAry = (String[][])application.getAttribute(getAppKey("route"));
    String tmpstr = "";
    if (tAry != null)
    {
      String tKey = cls.getString(argKey);
      for (int ti = 0; ti < tAry.length; ti ++)
      {
        if (tKey.equals(tAry[ti][0]))
        {
          tmpstr = tAry[ti][1];
          break;
        }
      }
    }
    return tmpstr;
  }

  public String getNGenre()
  {
    String tgenre = "";
    String tmyURL = getNURI();
    tmyURL = cls.getLRStr(tmyURL.toLowerCase(), "/", "leftr");
    String[] tmyURLAry = tmyURL.split(Pattern.quote("/"));
    int tmyURLAryLength = tmyURLAry.length;
    String tnroute = getNroute();
    if (tnroute.equals("greatgrandchild"))
    {
      if (tmyURLAryLength >= 4) tgenre = tmyURLAry[(tmyURLAryLength - 4)] + "/" + tmyURLAry[(tmyURLAryLength - 3)] + "/" + tmyURLAry[(tmyURLAryLength - 2)] + "/" + tmyURLAry[(tmyURLAryLength - 1)];
    }
    else if (tnroute.equals("grandchild"))
    {
      if (tmyURLAryLength >= 3) tgenre = tmyURLAry[(tmyURLAryLength - 3)] + "/" + tmyURLAry[(tmyURLAryLength - 2)] + "/" + tmyURLAry[(tmyURLAryLength - 1)];
    }
    else if (tnroute.equals("child"))
    {
      if (tmyURLAryLength >= 2) tgenre = tmyURLAry[(tmyURLAryLength - 2)] + "/" + tmyURLAry[(tmyURLAryLength - 1)];
    }
    else if (tnroute.equals("node"))
    {
      if (tmyURLAryLength >= 1) tgenre = tmyURLAry[(tmyURLAryLength - 1)];
    }
    tgenre= cls.getLRStr(tgenre ,"/"+ codeRoute,"leftr");
    return tgenre;
  }

  public String getRemortIP()
  {
    String tRemortIP = "";
    tRemortIP = request.getHeader("x-forwarded-for");
    if (tRemortIP == null) tRemortIP = request.getRemoteAddr();
    return tRemortIP;
  }

  public String getRequestURL()
  {
    String tmpstr = "";
    String tNURS = getNURS();
    tmpstr = request.getRequestURL().toString();
    if (!cls.isEmpty(tNURS)) tmpstr += "?" + tNURS;
    return tmpstr;
  }

  public String getRequestParameter(String argString)
  {
    String tString = argString;
    tString = decodeParameter(request.getParameter(tString));
    return tString;
  }

  public String getRequestParameters(String argString)
  {
    String tmpstr = "";
    String tString = argString;
    String[] tStringAry = request.getParameterValues(tString);
    if (tStringAry != null)
    {
      for (int ti = 0; ti < tStringAry.length; ti ++) tmpstr +=  tStringAry[ti] + ",";
      tmpstr = cls.getLRStr(tmpstr, ",", "leftr");
    }
    tmpstr = decodeParameter(tmpstr);
    return tmpstr;
  }

  public String getRequestUsParameter(String argString)
  {
    String tString = argString;
    tString = getRequestParameter(tString);
    try
    {
      tString = URLDecoder.decode(tString, charset);
    }
    catch(Exception e) {tString = "";}
    return tString;
  }

  public String getRequestUsParameters(String argString)
  {
    String tString = argString;
    tString = getRequestParameters(tString);
    try
    {
      tString = URLDecoder.decode(tString, charset);
    }
    catch(Exception e) {tString = "";}
    return tString;
  }

  public String urlencode(String argString)
  {
    String tString = argString;
    try
    {
      tString = URLEncoder.encode(tString, charset);
    }
    catch(Exception e) {tString = "";}
    return tString;
  }

  public void Init(Object argRequest, Object argResponse)
  {
    Object tRequest = argRequest;
    Object tResponse = argResponse;
    request = (HttpServletRequest)tRequest;
    response = (HttpServletResponse)tResponse;
    session = request.getSession();
    application = (ServletContext)session.getServletContext();
    //************************************************************
    appName = application.getInitParameter("appName");
    adminFolder = application.getInitParameter("adminFolder");
    charset = application.getInitParameter("charset");
    convert = application.getInitParameter("convert");
    connStr = application.getInitParameter("connStr");
    dbtype = application.getInitParameter("dbtype");
    default_language = application.getInitParameter("default_language");
    default_template = application.getInitParameter("default_template");
    default_theme = application.getInitParameter("default_theme");
    imagesRoute = application.getInitParameter("imagesRoute");
    codeRoute = application.getInitParameter("codeRoute");
    isApp = application.getInitParameter("isApp");
    linkMode = application.getInitParameter("linkMode");
    repath = application.getInitParameter("repath");
    xmlsfx = application.getInitParameter("xmlsfx");
    isDebug = application.getInitParameter("isDebug");
    //************************************************************
    sp = new sp(this);
    common = new common(this);
    //************************************************************
  }
}